package org.scboy.common.loads;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.coobi.sql.Coobi;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hutool.core.lang.ClassScanner;

/**
 * 权限初始化
 */
@Configuration
@AutoConfigureAfter(value = {Coobi.class,EntityFactory.class})
public class LoadPermission {

	@Value("${spring.application.name}")
	private String appName="";
	
	public List<Map<String, String>> init(){
		List<Map<String, String>> permissions=new ArrayList<Map<String,String>>();
		Set<Class<?>> scanner=ClassScanner.scanAllPackage();
		for (Class<?> clazz : scanner) {
			if(clazz.isAnnotationPresent(Menu.class)){
				//菜单名称
				String menuName=clazz.getAnnotation(Menu.class).value();
				//只允许一个url映射
				String url="/"+clazz.getAnnotation(RequestMapping.class).value()[0]+"/";
				Method [] methods= clazz.getMethods();
				for (Method method : methods) {
					String persmissionName=menuName;
					String httpUrl=url;
					if(method.isAnnotationPresent(Permission.class)){

						Map<String, String> perm=new HashMap<String, String>();
						
						String  permission=method.getAnnotation(Permission.class).value();
						String  node=method.getAnnotation(Permission.class).node();
						
						persmissionName+="."+permission;
						if(method.isAnnotationPresent(RequestMapping.class)){
							String actionUrl=method.getAnnotation(RequestMapping.class).value()[0];
							httpUrl+="/"+actionUrl;
							perm.put("httpMethod", "ALL");
						}else if(method.isAnnotationPresent(PostMapping.class)) {
							String actionUrl=method.getAnnotation(PostMapping.class).value()[0];
							httpUrl+="/"+actionUrl;
							perm.put("httpMethod", "POST");
						}else if(method.isAnnotationPresent(PutMapping.class)) {
							String actionUrl=method.getAnnotation(PutMapping.class).value()[0];
							httpUrl+="/"+actionUrl;
							perm.put("httpMethod", "PUT");
						}else if(method.isAnnotationPresent(GetMapping.class)) {
							String actionUrl=method.getAnnotation(GetMapping.class).value()[0];
							httpUrl+="/"+actionUrl;
							perm.put("httpMethod", "GET");
						}else if(method.isAnnotationPresent(DeleteMapping.class)) {
							String actionUrl=method.getAnnotation(DeleteMapping.class).value()[0];
							httpUrl+="/"+actionUrl;
							perm.put("httpMethod", "DELETE");
						}else if(method.isAnnotationPresent(PatchMapping.class)) {
							String actionUrl=method.getAnnotation(PatchMapping.class).value()[0];
							httpUrl+="/"+actionUrl;
							perm.put("httpMethod", "PATCH");
						}
						httpUrl=httpUrl.replaceAll("/+", "/").replaceAll("/$", "");
						perm.put("persmissionName", persmissionName);
						perm.put("persmissionuUrl", httpUrl);
						perm.put("node", node);
						permissions.add(perm);
					}
				}
			}
		}
		
		return permissions;
	}
}
